package com.soufang.ui;

import android.app.Activity;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.overlayutil.PoiOverlay;
import com.baidu.mapapi.search.core.CityInfo;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiDetailSearchOption;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.zxx.common.Common;

import android.os.Bundle;
import android.view.Window;
import android.widget.Toast;


public class mapActivity extends Activity implements
OnGetPoiSearchResultListener{
	
	// 百度地图控件
	private MapView mMapView = null;
	// 百度地图对象
	private BaiduMap mBaiduMap;
		
	private String mcity;
	private String mdepartment;
	private PoiSearch mPoiSearch = null;
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		mcity = getIntent().getStringExtra(Common.KEY_MAPINFO);
		mdepartment = getIntent().getStringExtra(Common.KEY_DEPARTINFO);
		SDKInitializer.initialize(getApplicationContext());
		setContentView(R.layout.map_layout);
		init();
	}

	/**
	 * 初始化方法
	 */
	private void init() {
		mMapView = (MapView) findViewById(R.id.bmapview);
		mPoiSearch = PoiSearch.newInstance();
		mPoiSearch.setOnGetPoiSearchResultListener(this);
		mBaiduMap = mMapView.getMap();
		searchButtonProcess(0);
		
	}
	@Override
	protected void onResume() {
		super.onResume();
		mMapView.onResume();
	}
	@Override
	protected void onPause() {
		super.onPause();
		mMapView.onPause();
	}
	@Override
	protected void onDestroy() {
		mMapView.onDestroy();
		mMapView = null;
		mPoiSearch.destroy();
		super.onDestroy();
	}
	
	/**
	 * 影响搜索按钮点击事件
	 * 
	 * @param v
	 */
	private void searchButtonProcess(int load_Index) {
		mPoiSearch.searchInCity((new PoiCitySearchOption())
				.city(mcity)
				.keyword(mdepartment)
				.pageNum(load_Index));
	}
	
	public void onGetPoiResult(PoiResult result) {
		if (result == null
				|| result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
			Toast.makeText(mapActivity.this, "未找到结果", Toast.LENGTH_LONG)
			.show();
			return;
		}
		if (result.error == SearchResult.ERRORNO.NO_ERROR) {
			mBaiduMap.clear();
			PoiOverlay overlay = new MyPoiOverlay(mBaiduMap);
			mBaiduMap.setOnMarkerClickListener(overlay);
			overlay.setData(result);
			overlay.addToMap();
			overlay.zoomToSpan();
			return;
		}
		if (result.error == SearchResult.ERRORNO.AMBIGUOUS_KEYWORD) {

			// 当输入关键字在本市没有找到，但在其他城市找到时，返回包含该关键字信息的城市列表
			String strInfo = "在";
			for (CityInfo cityInfo : result.getSuggestCityList()) {
				strInfo += cityInfo.city;
				strInfo += ",";
			}
			strInfo += "找到结果";
			Toast.makeText(mapActivity.this, strInfo, Toast.LENGTH_LONG)
					.show();
		}
	}
	
	public void onGetPoiDetailResult(PoiDetailResult result) {
		if (result.error != SearchResult.ERRORNO.NO_ERROR) {
			Toast.makeText(mapActivity.this, "抱歉，未找到结果", Toast.LENGTH_SHORT)
					.show();
		} else {
			Toast.makeText(mapActivity.this, result.getName() + ": " + result.getAddress(), Toast.LENGTH_SHORT)
			.show();
		}
	}
	
	private class MyPoiOverlay extends PoiOverlay {

		public MyPoiOverlay(BaiduMap baiduMap) {
			super(baiduMap);
		}

		@Override
		public boolean onPoiClick(int index) {
			super.onPoiClick(index);
			PoiInfo poi = getPoiResult().getAllPoi().get(index);
				mPoiSearch.searchPoiDetail((new PoiDetailSearchOption())
						.poiUid(poi.uid));
			return true;
		}
	}

}
